/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
* This describes the combined bitmaps (see TGIMI_BITMAP_WITH_TIMESTAMP) for sending
* over the network. Reason is simply to have minimized time difference in frames!
**/
#ifndef TGIMI_PROTOCOL_BITMAP_NET_VECTOR_H_
#define TGIMI_PROTOCOL_BITMAP_NET_VECTOR_H_

#include "TGIMI_BITMAP_WITH_TIMESTAMP.h"

#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include "bitestream.h"

#define BITMAP_BS_TAG 45671 ///< Just a random number

struct TGIMI_PROTOCOL_BITMAP_NET_VECTOR{
    private: 
	BiteStream *bs; ///< Bitestream formatted data is stored here
        bool isValid;
	
    public:
	TGIMI_PROTOCOL_BITMAP_NET_VECTOR()
          : bs (bsAlloc()),
            isValid (true)
        {
	    /*bs = bsAlloc();
            isValid=true;*/
	}
	
	~TGIMI_PROTOCOL_BITMAP_NET_VECTOR(){
	    bsFree(bs);
	}

	int32_t size(){
	    return (bs->length);
	}
	void clear(){
	    bsClear(bs);
	}
	
	int32_t add(TGIMI_BITMAP_WITH_TIMESTAMP *img){
	    bsAddBin(bs,BITMAP_BS_TAG, img->getNetPacket(), img->size());
	    return 0;
	}
	
	unsigned char* getNetData(){
	    // Finalize
	    bsFinalize(bs);
	    return (unsigned char*)bs->buf;
	}
	
	/**
	* Sets the data from the GIMI back to BiteStream
	*/
	int32_t handleNetPacket(char *data){
	    //fprintf(stderr,"Clear ");
            //clear();
            if(isValid){
              bsFree(bs);
              isValid = false;
            }
            //fprintf(stderr,"Decode \n");
            bs=bsDecode(data);
            
	    if(bs == NULL){
              isValid = false;
              return -1;
            }
	    fprintf(stderr,"Parsed some BS. Num of tags=%d Length=%d\n",
								bs->numtags, bs->length);
	    
            isValid=true;
            return 0;
           
	}
	
	/**
	* Copies the data to images
	*/
	int32_t getNextImage(TGIMI_BITMAP_WITH_TIMESTAMP *img){
	    bsTag *tag = bsNextTag(bs);
	    if(tag == NULL) return 0; /// No more Images
	    if(tag->tag != BITMAP_BS_TAG){
		fprintf(stderr,"WTF The TAG value does not match!!\n");
		fprintf(stderr,"Tag=%d Size=%d",tag->tag,tag->length);
		img = NULL;
		return -1;
	    }
	    img->handleNetPacket((const unsigned char *)tag->data,tag->length);
	    
	    return 1; ///<More images might still remain
	}
  private:
    // Prevent use of copy-constructors.
    TGIMI_PROTOCOL_BITMAP_NET_VECTOR(const TGIMI_PROTOCOL_BITMAP_NET_VECTOR& vec);
    const TGIMI_PROTOCOL_BITMAP_NET_VECTOR
        operator=(const TGIMI_PROTOCOL_BITMAP_NET_VECTOR& vec);
};

#endif


